<script lang="ts" setup>
import { PlusOutlined, LoadingOutlined } from '@ant-design/icons-vue';
import { ref, computed } from 'vue';
import type { UploadProps } from 'ant-design-vue';
import { message } from 'ant-design-vue';
import { uploadPictureUsingPost } from "@/api/pictureController";
import PictureVO = API.PictureVO;

declare namespace API {
  interface PictureVO {
    uid: string;
    name: string;
    status: 'done' | 'uploading' | 'error';
    url?: string;
    preview?: string;
    originFileObj?: File;
  }
}

interface Props {
  maxCount?: number;
  picture?: PictureVO;
  spaceId?: number
  onSuccess?: (file: API.PictureVO) => void;
}

const props = withDefaults(defineProps<Props>(), {
  maxCount: 8,
});

const uploading = ref(false);
const fileList = ref<UploadProps['fileList']>([]);
const loading = ref<boolean>(false);

const uploadButtonStyle = computed(() => ({
  background: uploading.value
      ? 'linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%)'
      : 'linear-gradient(135deg, #6366f1 0%, #4f46e5 100%)',
  opacity: uploading.value ? 0.7 : 1
}));

const beforeUpload = (file: UploadProps['fileList'][number]) => {
  const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
  if (!isJpgOrPng) {
    message.error('不支持上传该格式的图片，推荐 JPG 或 PNG');
  }
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isLt2M) {
    message.error('不能上传超过 2M 的图片');
  }
  return isJpgOrPng && isLt2M;
};

const handleUpload = async ({ file }: any) => {
  loading.value = true;
  try {
    const params: API.PictureUploadRequest = props.picture ? { id: props.picture.id } : {};
    params.spaceId = props.spaceId
    const res = await uploadPictureUsingPost(params, {}, file);
    if (res.data.code === 0 && res.data.data) {
      message.success('图片上传成功');
      props.onSuccess?.(res.data.data);
    } else {
      message.error('图片上传失败，' + res.data.message);
    }
  } catch (error) {
    message.error('图片上传失败');
  } finally {
    loading.value = false;
  }
};
</script>

<template>
  <div class="picture-upload">
    <a-upload
        list-type="picture-card"
        :show-upload-list="false"
        :custom-request="handleUpload"
        :before-upload="beforeUpload"
    >
      <div class="upload-content">
        <div v-if="picture?.url" class="image-preview">
          <img
              :src="picture.url"
              alt="uploaded image"
              class="preview-image"
          />
          <div class="hover-mask">
            <span class="hover-text">点击更换图片</span>
          </div>
        </div>
        <div v-else class="upload-card">
          <div class="upload-icon">
            <loading-outlined v-if="loading" class="spin-icon"/>
            <plus-outlined v-else/>
          </div>
          <div class="upload-text">点击或拖拽上传图片</div>
          <div class="upload-subtext">支持 JPG/PNG 格式，小于2MB</div>
        </div>
      </div>
    </a-upload>
  </div>
</template>

<style scoped lang="scss">
.picture-upload {
  :deep(.ant-upload) {
    width: 200px !important;
    height: 200px !important;
    border: 2px dashed #e2e8f0 !important;
    border-radius: 16px !important;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

    &:hover {
      border-color: #6366f1 !important;
      transform: translateY(-2px);
      box-shadow: 0 10px 15px -3px rgba(99, 102, 241, 0.1),
      0 4px 6px -2px rgba(99, 102, 241, 0.05);
    }
  }
}

.upload-content {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.image-preview {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 12px;
  overflow: hidden;
  transition: transform 0.3s ease;

  &:hover {
    transform: scale(1.02);

    .hover-mask {
      opacity: 1;
    }
  }

  .preview-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 12px;
  }

  .hover-mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(99, 102, 241, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.3s ease;
    border-radius: 12px;

    .hover-text {
      color: white;
      font-weight: 500;
      font-size: 14px;
      text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }
  }
}

.upload-card {
  padding: 24px;
  text-align: center;
  color: #6366f1;

  .upload-icon {
    font-size: 28px;
    margin-bottom: 12px;
    transition: transform 0.3s ease;

    :deep(svg) {
      fill: currentColor;
    }
  }

  .upload-text {
    font-weight: 500;
    margin-bottom: 4px;
    color: #4f46e5;
  }

  .upload-subtext {
    font-size: 12px;
    color: #64748b;
  }

  &:hover .upload-icon {
    transform: scale(1.1);
  }
}

.spin-icon {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

@media (max-width: 768px) {
  .picture-upload {
    :deep(.ant-upload) {
      width: 150px !important;
      height: 150px !important;
    }
  }

  .upload-card {
    padding: 16px;

    .upload-text {
      font-size: 14px;
    }

    .upload-subtext {
      font-size: 10px;
    }
  }
}
</style>
